﻿using System;
using System.IO;
using System.Text;
using System.Management.Automation;
using ExcelRefresh.Excel;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Threading;
using ExcelRefresh.Com;
using Microsoft.Office.Interop.Excel;
//using Microsoft.Office.Tools.Excel;
using Microsoft.Office.Core;
using Action = System.Action;

namespace ExcelRefresh
{
    //[Cmdlet("Refresh", "Workbook", SupportsShouldProcess = false, SupportsTransactions = false)]
    //public class ExcelCmdlet : Cmdlet
    //{
    //    private string _filePath;
    //    /// <summary>name of store</summary>
    //    [Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true)]
    //    public string FilePath
    //    {
    //        get { return _filePath; }
    //        set { _filePath = value; }
    //    }

    //    protected override void ProcessRecord()
    //    {
    //        try
    //        {
    //            if (!File.Exists(FilePath))
    //            {
    //                WriteError(new ErrorRecord(new FileNotFoundException("File does not exists", FilePath), null, ErrorCategory.ObjectNotFound, null));
    //                return;
    //            }

    //            ExcelHelpers.UsingWorkbook(FilePath, workBook =>
    //            {
    //                if (workBook.ReadOnly)
    //                {
    //                    WriteError(new ErrorRecord(new FileNotFoundException("File in use", FilePath), null, ErrorCategory.ResourceBusy, null));
    //                    return;
    //                }

    //                try
    //                {
    //                    ExcelHelpers.RefreshPowerPivot(workBook);
    //                    ExcelHelpers.RefreshDataConnections(workBook);
    //                    ExcelHelpers.RefreshPivotTables(workBook);
    //                    workBook.Save();
    //                }
    //                catch (Exception ex)
    //                {
    //                    WriteError(new ErrorRecord(ex, null, ErrorCategory.NotSpecified, null));
    //                    return;
    //                }
                    

    //            });
    //        }
    //        catch (Exception ex)
    //        {
    //            WriteError(new ErrorRecord(ex, null, ErrorCategory.OpenError, null));
    //        }
    //    }

        
    //}

}
